import logging

import tushare as ts
from django.contrib.auth.hashers import make_password

from apps.hangyefenlei.models import hangyefenlei
from mysystem.models import Users
from utils.export_excel import export_excel
from utils.jsonResponse import SuccessResponse, ErrorResponse
from utils.serializers import CustomModelSerializer
from utils.viewset import CustomModelViewSet

logger = logging.getLogger(__name__)


# Create your views here.

# ================================================= #
# ************** 后端用户管理 view  ************** #
# ================================================= #

class hangyefenleiManageSerializer(CustomModelSerializer):
    """
    用户管理-序列化器
    """

    class Meta:
        model = hangyefenlei
        read_only_fields = ["id"]
        fields = '__all__'  # 显式包含所有字段
        extra_kwargs = {
            'index_code': {'required': False},
            'industry_name': {'required': False},
        }


def syncHangyefenlei():
    ts.set_token('3dd4403b748878ed3722e0efeeda48b68a00696cb90f8323abf217d9')
    pro = ts.pro_api()
    df = pro.index_classify(level='L1', src='SW2021')
    logger.info(df)
    if df is not None and not df.empty:
        for _, value in df.to_dict('index').items():
            spu = hangyefenlei.objects.update_or_create(**value)
    return True


class hangyefenleiManageCreateSerializer(CustomModelSerializer):
    """
    用户管理-序列化器
    """

    # 新增重写
    def create(self, validated_data):
        if "password" in validated_data.keys():
            if validated_data['password']:
                validated_data['password'] = make_password(validated_data['password'])
        validated_data['identity'] = 2
        return super().create(validated_data)

    class Meta:
        model = hangyefenlei
        read_only_fields = ["id"]
        # exclude = ['role', 'post', 'dept']
        # extra_kwargs = {
        #     'post': {'required': False},
        #     'role': {'required': False},
        #     'name': {'required': False},
        #     'password': {'required': False},
        # }


class hangyefenleiSyncSerializer(CustomModelSerializer):

    # 新增重写
    def create(self, validated_data):
        if "password" in validated_data.keys():
            if validated_data['password']:
                validated_data['password'] = make_password(validated_data['password'])
        validated_data['identity'] = 2
        return super().create(validated_data)

    class Meta:
        model = hangyefenlei
        read_only_fields = ["id"]


class hangyefenleiViewSet(CustomModelViewSet):
    """
    后台用户管理 接口:
    """
    queryset = hangyefenlei.objects.order_by("-create_datetime")  # 排除管理员
    serializer_class = hangyefenleiManageSerializer
    create_serializer_class = hangyefenleiManageCreateSerializer
    filterset_fields = ('index_code', 'industry_name')
    search_fields = ('index_code', 'industry_name',)

    # filterset_class = UsersManageTimeFilter

    def disableuser(self, request, *args, **kwargs):
        """禁用用户"""
        instance = Users.objects.filter(id=kwargs.get('pk')).first()
        if instance:
            if instance.is_active:
                instance.is_active = False
            else:
                instance.is_active = True
            instance.save()
            return SuccessResponse(data=None, msg="修改成功")
        else:
            return ErrorResponse(msg="未获取到用户")

    def exportexecl(self, request):
        field_data = ['主键', '昵称', '手机号', '状态', '创建时间']
        queryset = self.filter_queryset(self.get_queryset())
        data = ExportUserManageSerializer(queryset, many=True).data
        return SuccessResponse(data=export_excel(request, field_data, data, '用户数据.xls'), msg='success')

# ================================================= #
# ************** 前端用户中心 view  ************** #
# ================================================= #

# 前端图片上传
# class uploadImagesView(APIView):
#     '''
#     前端图片上传
#     post:
#     【功能描述】前端图片上传</br>
#     【参数说明】无，需要登录携带token后才能调用</br>
#     '''
#     authentication_classes = [JWTAuthentication]
#     permission_classes = [IsAuthenticated]
#
#     def post(self, request, *args, **kwargs):
#         result = ImageUpload(request, "frontendimages")
#         if result['code'] == 200:
#             return SuccessResponse(data=result['img'], msg=result['msg'])
#         else:
#             return ErrorResponse(msg=result['msg'])
#
#
# class SetUserNicknameView(APIView):
#     """
#     修改昵称
#     post:
#     修改昵称
#     【参数】nickname:需要修改的用户新昵称
#     """
#     permission_classes = [IsAuthenticated]
#     authentication_classes = [JWTAuthentication]
#
#     # api文档参数
#
#     @swagger_auto_schema(operation_summary='app回收员修改昵称',
#                          # manual_parameters=[#GET请求需要
#                          #     # openapi.Parameter("nickname", openapi.IN_QUERY, description="要修改昵称", type=openapi.TYPE_STRING)
#                          # ],
#                          request_body=openapi.Schema(  # POST请求需要
#                              type=openapi.TYPE_OBJECT,
#                              required=['nickname'],
#                              properties={
#                                  'nickname': openapi.Schema(type=openapi.TYPE_STRING, description="要修改昵称"),
#                              },
#                          ),
#                          responses={200: 'success'},
#                          )
#     def post(self, request):
#         nickname = get_parameter_dic(request)['nickname']
#         if nickname is None:
#             return ErrorResponse(msg="昵称不能为空")
#         if not isinstance(nickname, str):
#             return ErrorResponse(msg='类型错误')
#         user = request.user
#         user.nickname = nickname
#         user.save()
#         return SuccessResponse(msg="success")
#
#
# # 前端app头像修改
# class ChangeAvatarView(APIView):
#     '''
#     前端app头像修改
#     post:
#     【功能描述】前端app头像修改</br>
#     【参数说明】无，需要登录携带token后才能调用</br>
#     '''
#     authentication_classes = [JWTAuthentication]
#     permission_classes = [IsAuthenticated]
#
#     def post(self, request, *args, **kwargs):
#         result = ImageUpload(request, "avatar")
#         if result['code'] == 200:
#             user = request.user
#             user.avatar = result['img'][0]
#             user.save()
#             return SuccessResponse(data=result['img'], msg=result['msg'])
#         else:
#             return ErrorResponse(msg=result['msg'])
#
#
# # 注销账号(标记已注销)
# class DestroyUserView(APIView):
#     '''
#     注销账号(标记已注销)
#     post:
#     【功能描述】注销账号(标记已注销)</br>
#     '''
#     authentication_classes = [JWTAuthentication]
#     permission_classes = [IsAuthenticated]
#
#     def post(self, request, *args, **kwargs):
#         user = request.user
#         if user.identity in [0, 1]:
#             return ErrorResponse(msg="该用户不支持注销")
#         if '(已注销)' in user.username:
#             return ErrorResponse(msg="该用户已注销或不支持注销")
#         with transaction.atomic():
#             randstr = getRandomSet(6)
#             user.username = user.username + "(已注销)" + randstr
#             user.mobile = user.mobile + "(已注销)" + randstr
#             user.is_delete = True
#             user.is_active = False
#             user.save()
#             OAuthWXUser.objects.filter(user=user).delete()
#             return SuccessResponse(data={}, msg="success")
#
#
# # 前端APP下载页面
# def downloadapp(request):
#     return render(request, "download-app/index.html")
